home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / gfx / show / svoUtah22.lha / svoUtahRLE / source / URT / lib / rle_open_f.c < prev    next >
C/C++ Source or Header  |  1991-08-09  |  3KB  |  150 lines

  1. /* 
  2.  * rle_open_f.c - Open a file with defaults.
  3.  * 
  4.  * Author :     Jerry Winters 
  5.  *         EECS Dept.
  6.  *         University of Michigan
  7.  * Date:    11/14/89
  8.  * Copyright (c) 1990, University of Michigan
  9.  */
  10.  
  11. #include "rle_config.h"
  12. #include <stdio.h>
  13. #ifdef USE_STRING_H
  14. #include <string.h>
  15. #else
  16. #include <strings.h>
  17. #endif
  18.  
  19. /* 
  20.  *  Purpose : Open a file for input or ouput as controled by the mode
  21.  *  parameter.  If no file name is specified (ie. file_name is null) then
  22.  *  a pointer to stdin or stdout will be returned.  The calling routine may
  23.  *  call this routine with a file name of "-".  For this case rle_open_f
  24.  *  will return a pointer to stdin or stdout depending on the mode.
  25.  *    If the user specifies a non-null file name and an I/O error occurs
  26.  *  when trying to open the file, rle_open_f will terminate execution with
  27.  *  an appropiate error message.
  28.  *
  29.  *  parameters
  30.  *   input:
  31.  *     prog_name:     name of the calling program.
  32.  *     file_name :     name of the file to open
  33.  *     mode :         either "r" for read or input file or "w" for write or
  34.  *                    output file
  35.  *
  36.  *   output:
  37.  *     a file pointer
  38.  * 
  39.  */
  40. FILE *
  41. rle_open_f_noexit( prog_name, file_name, mode ) 
  42. char *prog_name, *file_name, *mode;
  43. {
  44.     FILE *fp;
  45. #ifndef AMIGA
  46.     void perror();
  47. #endif
  48.     char *err_str;
  49.     register char *cp;
  50.     char *combuf;
  51.  
  52. #ifdef STDIO_NEEDS_BINARY
  53.     char mode_string[32];    /* Should be enough. */
  54.  
  55.     /* Concatenate a 'b' onto the mode. */
  56.     mode_string[0] = mode[0];
  57.     mode_string[1] = 'b';
  58.     strcpy( mode_string + 2, mode + 1 );
  59.     mode = mode_string;
  60. #endif
  61.  
  62.     if ( *mode == 'w' || *mode == 'a' )
  63.     fp = stdout;     /* Set the default value */
  64.     else
  65.     fp = stdin;
  66.     
  67.     if ( file_name != NULL && strcmp( file_name, "-" ) != 0 )
  68.     {
  69. #ifndef    NO_OPEN_PIPES
  70.     /*  Real file, not stdin or stdout.  If name ends in ".Z",
  71.      *  pipe from/to un/compress (depending on r/w mode).
  72.      *  
  73.      *  If it starts with "|", popen that command.
  74.      */
  75.  
  76.     cp = file_name + strlen( file_name ) - 2;
  77.     /* Pipe case. */
  78.     if ( *file_name == '|' )
  79.     {
  80.         if ( (fp = popen( file_name + 1, mode )) == NULL )
  81.         {
  82.         err_str = "%s: can't invoke <<%s>> for %s: ";
  83.         goto err;
  84.         }
  85.     }
  86.  
  87.     /* Compress case. */
  88.     else if ( cp > file_name && *cp == '.' && *(cp + 1) == 'Z' )
  89.     {
  90.         combuf = (char *)malloc( 20 + strlen( file_name ) );
  91.         if ( combuf == NULL )
  92.         {
  93.         err_str = "%s: out of memory opening (compressed) %s for %s";
  94.         goto err;
  95.         }
  96.  
  97.         if ( *mode == 'w' )
  98.         sprintf( combuf, "compress > %s", file_name );
  99.         else if ( *mode == 'a' )
  100.         sprintf( combuf, "compress >> %s", file_name );
  101.         else
  102.         sprintf( combuf, "compress -d < %s", file_name );
  103.  
  104.         fp = popen( combuf, mode );
  105.         free( combuf );
  106.  
  107.         if ( fp == NULL )
  108.         {
  109.         err_str =
  110.     "%s: can't invoke 'compress' program, trying to open %s for %s";
  111.         goto err;
  112.         }
  113.     }
  114.  
  115.     /* Ordinary, boring file case. */
  116.     else
  117. #endif /* !NO_OPEN_PIPES */
  118.         if ( (fp = fopen(file_name, mode)) == NULL )
  119.         {
  120.         err_str = "%s: can't open %s for %s: ";
  121.         goto err;
  122.         }
  123.     }
  124.  
  125.     return fp;
  126.  
  127. err:
  128.     fprintf( stderr, err_str,
  129.          prog_name, file_name,
  130.          (*mode == 'w') ? "output" :
  131.          (*mode == 'a') ? "append" :
  132.          "input" );
  133.     perror( "" );
  134.     return NULL;
  135.  
  136. }
  137.  
  138. FILE *
  139. rle_open_f( prog_name, file_name, mode )
  140. char *prog_name, *file_name, *mode;
  141. {
  142.     FILE *fp;
  143.  
  144.     if ( (fp = rle_open_f_noexit( prog_name, file_name, mode )) == NULL )
  145.     exit( -1 );
  146.  
  147.     return fp;
  148. }
  149.  
  150.